'use strict';

angular.module('app')
	.controller('platfromInfoController', [ '$rootScope', '$scope', '$http', '$state',
	                                function($rootScope, $scope, $http, $state) {
		$scope.title = '平台';
        $scope.param = { };
        $scope.loading = false;
        
		$scope.search = function () {
	        $scope.loading = true;
			$.ajax({
				url : $scope.app.project_name + '/platfrom/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.searchRe = function () {
	        $scope.loading = true;
	        $scope.param.pageNum = 1;
	        
	        var createStartTime = $("#createStartTime").val();
	        var createEndTime = $("#createEndTime").val();
	        $scope.param.createStartTime = createStartTime;
	        $scope.param.createEndTime = createEndTime;

	        var approveStartTime = $("#approveStartTime").val();
	        var approveEndTime = $("#approveEndTime").val();
	        $scope.param.approveStartTime = approveStartTime;
	        $scope.param.approveEndTime = approveEndTime;

	        $.ajax({
				url : $scope.app.project_name + '/platfrom/read/list',
				data: $scope.param
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result.data;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		
		$scope.search();
		
		$scope.clearSearch = function() {
			$scope.param.keyword = null;
			$scope.search();
		}


		$scope.selected = [];
		var updateSelected = function (action, contact) {
			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
				$scope.selected.push(contact.id);
				contact.checked = true;
			}

			if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
				$scope.selected.splice($scope.selected.indexOf(contact.id), 1);
				contact.checked = false;
			}
		};

		$scope.updateSelection = function ($event, contact) {
			var checkbox = $event.target;
			var action = (checkbox.checked ? 'add' : 'remove');
			if (action == 'add' && $scope.selected.indexOf(contact.id) == -1){
				$scope.selected.push(contact.id);
				contact.checked = true;
			}
			if (action == 'remove' && $scope.selected.indexOf(contact.id) != -1){
				$scope.selected.splice($scope.selected.indexOf(contact.id), 1);
				contact.checked = false;
			}
		};
		// 全选操作
		$scope.selectAll = function ($event) {
			var checkbox = $event.target;
			var action = (checkbox.checked ? 'add' : 'remove');
			for (var i = 0; i < $scope.pageInfo.records.length; i++) {
				var contact = $scope.pageInfo.records[i];
				updateSelected(action, contact);
			}
		};

		$scope.isSelected = function (id) {
			return $scope.selected.indexOf(id) >= 0;
		};


		$scope.approve = function(id, state) {
            var ids = [];

            if (id == null || id == '') {
                ids = $scope.selected;
            } else {
                ids.push(id);
            }

            var idStr = ids.join(',');
            if (idStr == null || idStr == '') {
                $scope.alertModal({"content":"请选择需要审核的记录"});
                return;
            }
            //判断是否有待审核的并记录有多少条
            var count = 0;
            var nocount = 0;
            angular.forEach($scope.pageInfo.records,function (item) {
                if(item.state=='1'&&item.checked){
                    count++;
                }else if(item.checked&&item.state!='1') {
                    nocount++;
                }
            })
            if(nocount>0){
                $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                return;
            }

			$scope.alertMessage({
                text:"确定做此操作吗？", //内容
                btnCancel:true, //取消按钮
                btnConfirm:true //确认按钮
            }).then(function () {
    			$.ajax({
    				url : $scope.app.project_name + '/platfrom/update/approve',
    				data: {'ids': idStr, 'state' : state, 'approveFailReason' : ''},
    				traditional: true //traditional 为true阻止深度序列化
    			}).then(function(result) {
    		        $scope.loading = false;
    				if (result.httpCode == 200) {
    					$scope.param.keyword = null;
    					$scope.selected = [];
    					$scope.search();
    				} else {
    					$scope.msg = result.msg;
    				}
    			});
            })
		}

		$scope.closeplf = function (id, state) {


            var ids = [];

            if (id == null || id == '') {
                ids = $scope.selected;
            } else {
                ids.push(id);
            }

            var idStr = ids.join(',');
            if (idStr == null || idStr == '') {
                $scope.alertModal({"content":"请选择需要操作的记录"});
                return;
            }

            $scope.alertMessage({
                text:"确定做此操作吗？", //内容
                btnCancel:true, //取消按钮
                btnConfirm:true //确认按钮
            }).then(function () {
                $.ajax({
                    url : $scope.app.project_name + '/platfrom/update/closeplf',
                    data: {'ids': idStr, 'state' : state},
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.alertModal({"content":"操作成功"});
                        $scope.param.keyword = null;
                        $scope.selected = [];
                        $scope.search();
                    } else {
                        $scope.alertModal({"content":"操作失败"});
                    }
                });
            })

        }

		$scope.refuse = function (id, state) {
		    $scope.platfromInfo = {};
		    
		    var ids = [];
			
			if (id == null || id == '') {
				ids = $scope.selected;
			} else {
				ids.push(id);
			}

			var idStr = ids.join(',');
			if (idStr == null || idStr == '') {
				$scope.alertModal({"content":"请选择需要审核的记录"});
                return;
			} else {

                //判断是否有待审核的并记录有多少条
                var count = 0;
                var nocount = 0;
                angular.forEach($scope.pageInfo.records,function (item) {
                    if(item.state=='1'&&item.checked){
                        count++;
                    }else if(item.checked&&item.state!='1') {
                        nocount++;
                    }
                })
                if(nocount>0){
                    $scope.alertModal({"content":"待审核"+count+"条,不符合操作条件的"+nocount+"条"});
                    return;
                }
				$scope.platfromInfo.ids = idStr;
	            $scope.platfromInfo.state = state;

	            $('#refusePlatfrom').modal('show', true);
			}
        }
		
		$scope.saveRefuse = function () {
            if ($scope.platfromInfo.approveFailReason == "" || $scope.platfromInfo.approveFailReason == null) {
                $scope.alertModal({"content":"请选择拒绝原因"});
                $('#refusePlatfrom').modal('hide');
                return;
            }

            $.ajax({
                url : $scope.app.project_name + '/platfrom/update/approve',
                data: $scope.platfromInfo
            }).then(function(result) {
                $scope.loading = false;
                if (result.httpCode == 200) {
                	$('#refusePlatfrom').modal('hide');
                    $scope.alertModal({"content":"操作成功"});
                    
                	$scope.param.keyword = null;
					$scope.selected = [];
					$scope.search();
                } else {
                    $('#refusePlatfrom').modal('hide');
                    $scope.alertModal({"content":"操作失败"});
                }
            });
        }
		
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum = page;
            $scope.search();
        };
        
        $scope.industryQualifications = [];
        $scope.preview = function(industryQualification){
        	if (industryQualification != null && industryQualification != '') {
				$scope.industryQualifications = industryQualification.split('|');
			}
        	$scope.firstApply = true;
        }
} ]);